home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpsdu.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.9 KB  |  417 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpsdu.c,v 1.7.2.1 2000/11/13 05:28:54 rayjj Exp $ */
  20. /* Common utilities for PostScript and PDF writers */
  21. #include "stdio_.h"        /* for FILE for jpeglib.h */
  22. #include "jpeglib_.h"        /* for sdct.h */
  23. #include "memory_.h"
  24. #include <stdlib.h>        /* for qsort */
  25. #include "gx.h"
  26. #include "gserrors.h"
  27. #include "gdevpsdf.h"
  28. #include "gxfont.h"
  29. #include "scanchar.h"
  30. #include "strimpl.h"
  31. #include "sa85x.h"
  32. #include "scfx.h"
  33. #include "sdct.h"
  34. #include "sjpeg.h"
  35. #include "spprint.h"
  36. #include "sstring.h"
  37.  
  38. /* Structure descriptors */
  39. public_st_device_psdf();
  40. public_st_psdf_binary_writer();
  41.  
  42. /* Standard color command names. */
  43. const psdf_set_color_commands_t psdf_set_fill_color_commands = {
  44.     "g", "rg", "k", "cs", "sc", "scn"
  45. };
  46. const psdf_set_color_commands_t psdf_set_stroke_color_commands = {
  47.     "G", "RG", "K", "CS", "SC", "SCN"
  48. };
  49.  
  50.  
  51. /* Define parameter-setting procedures. */
  52. extern stream_state_proc_put_params(s_DCTE_put_params, stream_DCT_state);
  53.  
  54. /* ---------------- Vector implementation procedures ---------------- */
  55.  
  56. int
  57. psdf_setlinewidth(gx_device_vector * vdev, floatp width)
  58. {
  59.     pprintg1(gdev_vector_stream(vdev), "%g w\n", width);
  60.     return 0;
  61. }
  62.  
  63. int
  64. psdf_setlinecap(gx_device_vector * vdev, gs_line_cap cap)
  65. {
  66.     pprintd1(gdev_vector_stream(vdev), "%d J\n", cap);
  67.     return 0;
  68. }
  69.  
  70. int
  71. psdf_setlinejoin(gx_device_vector * vdev, gs_line_join join)
  72. {
  73.     pprintd1(gdev_vector_stream(vdev), "%d j\n", join);
  74.     return 0;
  75. }
  76.  
  77. int
  78. psdf_setmiterlimit(gx_device_vector * vdev, floatp limit)
  79. {
  80.     pprintg1(gdev_vector_stream(vdev), "%g M\n", limit);
  81.     return 0;
  82. }
  83.  
  84. int
  85. psdf_setdash(gx_device_vector * vdev, const float *pattern, uint count,
  86.          floatp offset)
  87. {
  88.     stream *s = gdev_vector_stream(vdev);
  89.     int i;
  90.  
  91.     pputs(s, "[ ");
  92.     for (i = 0; i < count; ++i)
  93.     pprintg1(s, "%g ", pattern[i]);
  94.     pprintg1(s, "] %g d\n", offset);
  95.     return 0;
  96. }
  97.  
  98. int
  99. psdf_setflat(gx_device_vector * vdev, floatp flatness)
  100. {
  101.     pprintg1(gdev_vector_stream(vdev), "%g i\n", flatness);
  102.     return 0;
  103. }
  104.  
  105. int
  106. psdf_setlogop(gx_device_vector * vdev, gs_logical_operation_t lop,
  107.           gs_logical_operation_t diff)
  108. {
  109. /****** SHOULD AT LEAST DETECT SET-0 & SET-1 ******/
  110.     return 0;
  111. }
  112.  
  113. int
  114. psdf_setfillcolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
  115. {
  116.     return psdf_set_color(vdev, pdc, &psdf_set_fill_color_commands);
  117. }
  118.  
  119. int
  120. psdf_setstrokecolor(gx_device_vector * vdev, const gx_drawing_color * pdc)
  121. {
  122.     return psdf_set_color(vdev, pdc, &psdf_set_stroke_color_commands);
  123. }
  124.  
  125. int
  126. psdf_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1, fixed y1,
  127.         gx_path_type_t type)
  128. {
  129.     int code = (*vdev_proc(vdev, beginpath)) (vdev, type);
  130.  
  131.     if (code < 0)
  132.     return code;
  133.     pprintg4(gdev_vector_stream(vdev), "%g %g %g %g re\n",
  134.          fixed2float(x0), fixed2float(y0),
  135.          fixed2float(x1 - x0), fixed2float(y1 - y0));
  136.     return (*vdev_proc(vdev, endpath)) (vdev, type);
  137. }
  138.  
  139. int
  140. psdf_beginpath(gx_device_vector * vdev, gx_path_type_t type)
  141. {
  142.     return 0;
  143. }
  144.  
  145. int
  146. psdf_moveto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x, floatp y,
  147.         gx_path_type_t type)
  148. {
  149.     pprintg2(gdev_vector_stream(vdev), "%g %g m\n", x, y);
  150.     return 0;
  151. }
  152.  
  153. int
  154. psdf_lineto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x, floatp y,
  155.         gx_path_type_t type)
  156. {
  157.     pprintg2(gdev_vector_stream(vdev), "%g %g l\n", x, y);
  158.     return 0;
  159. }
  160.  
  161. int
  162. psdf_curveto(gx_device_vector * vdev, floatp x0, floatp y0,
  163.        floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3,
  164.          gx_path_type_t type)
  165. {
  166.     if (x1 == x0 && y1 == y0)
  167.     pprintg4(gdev_vector_stream(vdev), "%g %g %g %g v\n",
  168.          x2, y2, x3, y3);
  169.     else if (x3 == x2 && y3 == y2)
  170.     pprintg4(gdev_vector_stream(vdev), "%g %g %g %g y\n",
  171.          x1, y1, x2, y2);
  172.     else
  173.     pprintg6(gdev_vector_stream(vdev), "%g %g %g %g %g %g c\n",
  174.          x1, y1, x2, y2, x3, y3);
  175.     return 0;
  176. }
  177.  
  178. int
  179. psdf_closepath(gx_device_vector * vdev, floatp x0, floatp y0,
  180.            floatp x_start, floatp y_start, gx_path_type_t type)
  181. {
  182.     pputs(gdev_vector_stream(vdev), "h\n");
  183.     return 0;
  184. }
  185.  
  186. /* endpath is deliberately omitted. */
  187.  
  188. /* ---------------- Utilities ---------------- */
  189.  
  190. gx_color_index
  191. psdf_adjust_color_index(gx_device_vector *vdev, gx_color_index color)
  192. {
  193.     /*
  194.      * Since gx_no_color_index is all 1's, we can't represent
  195.      * a CMYK color consisting of full ink in all 4 components.
  196.      * However, this color must be available for registration marks.
  197.      * gxcmap.c fudges this by changing the K component to 254;
  198.      * undo this fudge here.
  199.      */
  200.     return (color == (gx_no_color_index ^ 1) ? gx_no_color_index : color);
  201. }
  202.  
  203. /*
  204.  * Since we only have 8 bits of color to start with, round the
  205.  * values to 3 digits for more compact output.
  206.  */
  207. private double
  208. round_byte_color(int cv)
  209. {
  210.     return (int)(cv * (1000.0 / 255.0) + 0.5) / 1000.0;
  211. }
  212. int
  213. psdf_set_color(gx_device_vector * vdev, const gx_drawing_color * pdc,
  214.            const psdf_set_color_commands_t *ppscc)
  215. {
  216.     const char *setcolor;
  217.  
  218.     if (!gx_dc_is_pure(pdc))
  219.     return_error(gs_error_rangecheck);
  220.     {
  221.     stream *s = gdev_vector_stream(vdev);
  222.     gx_color_index color =
  223.         psdf_adjust_color_index(vdev, gx_dc_pure_color(pdc));
  224.     /*
  225.      * Normally we would precompute all of v0 .. v3, but gcc 2.7.2.3
  226.      * generates incorrect code for Intel CPUs if we do this.  The code
  227.      * below is longer, but does less computation in some cases.
  228.      */
  229.     double v3 = round_byte_color(color & 0xff);
  230.  
  231.     switch (vdev->color_info.num_components) {
  232.     case 4:
  233.         /* if (v0 == 0 && v1 == 0 && v2 == 0 && ...) */
  234.         if ((color & (0xffffff << 8)) == 0 && ppscc->setgray != 0) {
  235.         v3 = 1.0 - v3;
  236.         goto g;
  237.         }
  238.         pprintg4(s, "%g %g %g %g", round_byte_color(color >> 24),
  239.              round_byte_color((color >> 16) & 0xff),
  240.              round_byte_color((color >> 8) & 0xff), v3);
  241.         setcolor = ppscc->setcmykcolor;
  242.         break;
  243.     case 3:
  244.         /* if (v1 == v2 && v2 == v3 && ...) */
  245.         if (!((color ^ (color >> 8)) & 0xffff) && ppscc->setgray != 0)
  246.         goto g;
  247.         pprintg3(s, "%g %g %g", round_byte_color((color >> 16) & 0xff),
  248.              round_byte_color((color >> 8) & 0xff), v3);
  249.         setcolor = ppscc->setrgbcolor;
  250.         break;
  251.     case 1:
  252.     g:
  253.         pprintg1(s, "%g", v3);
  254.         setcolor = ppscc->setgray;
  255.         break;
  256.     default:        /* can't happen */
  257.         return_error(gs_error_rangecheck);
  258.     }
  259.     if (setcolor)
  260.         pprints1(s, " %s\n", setcolor);
  261.     }
  262.     return 0;
  263. }
  264.  
  265. /* ---------------- Binary data writing ---------------- */
  266.  
  267. /* Begin writing binary data. */
  268. int
  269. psdf_begin_binary(gx_device_psdf * pdev, psdf_binary_writer * pbw)
  270. {
  271.     gs_memory_t *mem = pbw->memory = pdev->v_memory;
  272.  
  273.     pbw->target = pdev->strm;
  274.     pbw->dev = pdev;
  275.     /* If not binary, set up the encoding stream. */
  276.     if (!pdev->binary_ok) {
  277. #define BUF_SIZE 100        /* arbitrary */
  278.     byte *buf = gs_alloc_bytes(mem, BUF_SIZE, "psdf_begin_binary(buf)");
  279.     stream_A85E_state *ss = (stream_A85E_state *)
  280.         s_alloc_state(mem, s_A85E_template.stype,
  281.               "psdf_begin_binary(stream_state)");
  282.     stream *s = s_alloc(mem, "psdf_begin_binary(stream)");
  283.  
  284.     if (buf == 0 || ss == 0 || s == 0) {
  285.         gs_free_object(mem, s, "psdf_begin_binary(stream)");
  286.         gs_free_object(mem, ss, "psdf_begin_binary(stream_state)");
  287.         gs_free_object(mem, buf, "psdf_begin_binary(buf)");
  288.         return_error(gs_error_VMerror);
  289.     }
  290.     ss->template = &s_A85E_template;
  291.     s_init_filter(s, (stream_state *)ss, buf, BUF_SIZE, pdev->strm);
  292. #undef BUF_SIZE
  293.     pbw->strm = pbw->A85E = s;
  294.     } else {
  295.     pbw->A85E = 0;        /* for GC */
  296.     pbw->strm = pdev->strm;
  297.     }
  298.     return 0;
  299. }
  300.  
  301. /* Add an encoding filter.  The client must have allocated the stream state, */
  302. /* if any, using pdev->v_memory. */
  303. int
  304. psdf_encode_binary(psdf_binary_writer * pbw, const stream_template * template,
  305.            stream_state * ss)
  306. {
  307.     return (s_add_filter(&pbw->strm, template, ss, pbw->memory) == 0 ?
  308.         gs_note_error(gs_error_VMerror) : 0);
  309. }
  310.  
  311. /*
  312.  * Acquire parameters, and optionally set up the filter for, a DCTEncode
  313.  * filter.  This is a separate procedure so it can be used to validate
  314.  * filter parameters when they are set, rather than waiting until they are
  315.  * used.  pbw = NULL means just set up the stream state.
  316.  */
  317. int
  318. psdf_DCT_filter(gs_param_list *plist /* may be NULL */,
  319.         stream_state /*stream_DCTE_state*/ *st,
  320.         int Columns, int Rows, int Colors,
  321.         psdf_binary_writer *pbw /* may be NULL */)
  322. {
  323.     stream_DCT_state *const ss = (stream_DCT_state *) st;
  324.     gs_memory_t *mem = st->memory;
  325.     jpeg_compress_data *jcdp;
  326.     gs_c_param_list rcc_list;
  327.     int code;
  328.  
  329.     /*
  330.      * "Wrap" the actual Dict or ACSDict parameter list in one that
  331.      * sets Rows, Columns, and Colors.
  332.      */
  333.     gs_c_param_list_write(&rcc_list, mem);
  334.     if ((code = param_write_int((gs_param_list *)&rcc_list, "Rows",
  335.                     &Rows)) < 0 ||
  336.         (code = param_write_int((gs_param_list *)&rcc_list, "Columns",
  337.                     &Columns)) < 0 ||
  338.         (code = param_write_int((gs_param_list *)&rcc_list, "Colors",
  339.                     &Colors)) < 0
  340.         ) {
  341.         goto rcc_fail;
  342.     }
  343.     gs_c_param_list_read(&rcc_list);
  344.     if (plist)
  345.         gs_c_param_list_set_target(&rcc_list, plist);
  346.     /* Allocate space for IJG parameters. */
  347.     jcdp = (jpeg_compress_data *)
  348.         gs_alloc_bytes_immovable(mem, sizeof(*jcdp), "zDCTE");
  349.     if (jcdp == 0)
  350.         return_error(gs_error_VMerror);
  351.     ss->data.compress = jcdp;
  352.     jcdp->memory = ss->jpeg_memory = mem;    /* set now for allocation */
  353.     if ((code = gs_jpeg_create_compress(ss)) < 0)
  354.         goto dcte_fail;    /* correct to do jpeg_destroy here */
  355.     /* Read parameters from dictionary */
  356.     s_DCTE_put_params((gs_param_list *)&rcc_list, ss); /* ignore errors */
  357.     /* Create the filter. */
  358.     jcdp->template = s_DCTE_template;
  359.     /* Make sure we get at least a full scan line of input. */
  360.     ss->scan_line_size = jcdp->cinfo.input_components *
  361.         jcdp->cinfo.image_width;
  362.     jcdp->template.min_in_size =
  363.         max(s_DCTE_template.min_in_size, ss->scan_line_size);
  364.     /* Make sure we can write the user markers in a single go. */
  365.     jcdp->template.min_out_size =
  366.         max(s_DCTE_template.min_out_size, ss->Markers.size);
  367.     if (pbw)
  368.         code = psdf_encode_binary(pbw, &jcdp->template, st);
  369.     if (code >= 0) {
  370.         gs_c_param_list_release(&rcc_list);
  371.         return 0;
  372.     }
  373.     dcte_fail:
  374.     gs_jpeg_destroy(ss);
  375.     gs_free_object(mem, jcdp, "setup_image_compression");
  376.     rcc_fail:
  377.     gs_c_param_list_release(&rcc_list);
  378.     return code;
  379. }
  380.  
  381. /* Add a 2-D CCITTFax encoding filter. */
  382. /* Set EndOfBlock iff the stream is not ASCII85 encoded. */
  383. int
  384. psdf_CFE_binary(psdf_binary_writer * pbw, int w, int h, bool invert)
  385. {
  386.     gs_memory_t *mem = pbw->memory;
  387.     const stream_template *template = &s_CFE_template;
  388.     stream_CFE_state *st =
  389.     gs_alloc_struct(mem, stream_CFE_state, template->stype,
  390.             "psdf_CFE_binary");
  391.     int code;
  392.  
  393.     if (st == 0)
  394.     return_error(gs_error_VMerror);
  395.     (*template->set_defaults) ((stream_state *) st);
  396.     st->K = -1;
  397.     st->Columns = w;
  398.     st->Rows = 0;
  399.     st->BlackIs1 = !invert;
  400.     st->EndOfBlock = pbw->strm->state->template != &s_A85E_template;
  401.     code = psdf_encode_binary(pbw, template, (stream_state *) st);
  402.     if (code < 0)
  403.     gs_free_object(mem, st, "psdf_CFE_binary");
  404.     return code;
  405. }
  406.  
  407. /* Finish writing binary data. */
  408. int
  409. psdf_end_binary(psdf_binary_writer * pbw)
  410. {
  411.     int code = s_close_filters(&pbw->strm, pbw->target);
  412.  
  413.     /* s_close_filters freed the A85E stream, if any. */
  414.     pbw->A85E = 0;        /* for GC */
  415.     return code;
  416. }
  417.